/// <reference path="../typings/index.d.ts" />
/// <reference path="../src/userService.ts" />
/// <reference path="../src/tms.ts" />

describe('userService', () => {
    var injector: ng.auto.IInjectorService;
    var service: IUserService;

    beforeEach(() => {
        injector = angular.injector(['ng', 'tms']);
        service = injector.get<IUserService>('userService');
    });

    //常规无条件获取
    it('get user list normal', (done) => {
        service.getList(null, null, null, null, 0, 10).then((res) => {
            expect(res.data.total).toBeGreaterThan(0);
            expect(res.data.list.length).toBe(10);
            expect(res.data.extData).toBe('||||0|10');
            done();
        }, (err) => {
            done.fail(err);
        });
    });

    //获取用户名包含1的用户列表
    it('get user list by userName \'1\'', (done) => {
        service.getList('1', null, null, null, 0, 10).then((res) => {
            expect(res.data.total).toBeGreaterThan(0);
            expect(res.data.list.length).toBe(10);
            expect(res.data.extData).toBe("1||||0|10");
            done();
        }, (err) => {
            done.fail(err);
        });
    });

    //获取手机号包含135的用户列表
    it('get user list by phoneNumber \'135\'', (done) => {
        service.getList(null, '135', null, null, 0, 10).then((res) => {
            expect(res.data.total).toBeGreaterThan(0);
            expect(res.data.list.length).toBe(10);
            expect(res.data.extData).toBe("|135|||0|10");
            done();
        }, (err) => {
            done.fail(err);
        });
    });

    //获取用户禁用的用户列表
    it('get user list by disabled ', (done) => {
        service.getList(null, null, false, null, 0, 10).then((res) => {
            expect(res.data.total).toBe(10);
            expect(res.data.list.length).toBe(10);
            expect(res.data.extData).toBe("||false||0|10");
            done();
        }, (err) => {
            done.fail(err);
        });
    });

    //获取用户名包含1且手机号包含135的用户列表
    it('get user list by userName \'1\' and phoneNumber \'135\'', (done) => {
        service.getList('1', '135', null, null, 0, 10).then((res) => {
            expect(res.data.total).toBe(10);
            expect(res.data.list.length).toBe(10);
            expect(res.data.extData).toBe("1|135|||0|10")
            done();
        }, (err) => {
            done.fail(err);
        });
    });

    //获取用户名包含1，手机号包含135，禁用且用户组编号为1的用户列表
    it('get user list by userName \'1\' and phoneNumber \'135\' and disabled and userGroupId \'1\'', (done) => {
        service.getList('1', '135', false, '1', 0, 10).then((res) => {
            expect(res.data.total).toBe(10);
            expect(res.data.list.length).toBe(10);
            expect(res.data.extData).toBe("1|135|false|1|0|10");
            done();
        }, (err) => {
            done.fail(err);
        });
    });

    //获取详情
    it("get detail", (done) => {
        service.getDetail("1").then((res) => {
            expect(res.data.userName).toBe("detail");
            expect(res.data.email).toBe("detail@qq.com");
            expect(res.data.phoneNumber).toBe("15854565852");
            expect(res.data.isEnabled).toBe(true);
            expect(res.data.userGroupId).toBe("1");
            expect(res.data.roles.length).toBe(2);
            expect(res.data.extData).toBe("1");
            done();
        }, (err) => {
            done.fail(err);
        });
    });

    //添加用户
    it('add user', (done) => {
        service.add("1", "1", "1", "1@qq.com", "135", false, "1", ["1", "2"]).then((res) => {
            expect(res.data.extData).toBe("1|1|1|1@qq.com|135|false|1|2");
            done();
        }, (err) => {
            done.fail(err);
        });
    });

    //编辑用户
    it("edit user", (done) => {
        service.edit("1", "2", "2", "2@qq.com", "137", false, "2", ["2", "3", "4"]).then((res) => {
            expect(res.data.extData).toBe("1|2|2|2@qq.com|137|false|2|3");
            done();
        }, (err) => {
            done.fail(err);
        });
    });

    //修改密码
    it("change password", (done) => {
        service.updatePassword("1", "1", "2", "2").then((res) => {
            expect(res.data.extData).toBe("1|1|2|2");
            done();
        }, (err) => {
            done.fail(err);
        });
    });

    //获取容云Token
    it("get rong token", (done) => {
        service.getRongToken().then((res) => {
            expect(res.data.extData).toBe("111");
            done();
        }, (err) => {
            done.fail(err);
        });
    });
});